package su.kozhevnikov.nookbrary.views;

import su.kozhevnikov.nookbrary.Util;
import su.kozhevnikov.nookbrary.enums.ItemType;
import su.kozhevnikov.nookbrary.interfaces.IItem;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class ItemView extends View
{
	static final int WIDTH = 584;
	static final int HEIGHT = 67;
	protected Paint _ptitle, _pauth, _pseries, 
		_pindex, _psepline, _pboldline, _pfiletype;
	int _index;
	IItem _item;
	int[] _positions=POSTAB1;
	
    public ItemView(Context context,int index) 
    { 
        super(context); 
        
        _index = index;
 
        Paint p = new Paint();
        p.setColor(Color.BLACK); 
        p.setStrokeWidth(1); 
        p.setStyle(Style.FILL);
        p.setTextSize(30);
        p.setAntiAlias(true);

        _pindex = p;

        _ptitle = new Paint(p);
        //_ptitle.setTextSize(26);
        _ptitle.setTypeface(Typeface.DEFAULT_BOLD);

        _pseries = new Paint(p);
        _pseries.setTextSize(15);
        
        _pauth = new Paint(p);
        _pauth.setTextSize(20);

        _pfiletype = new Paint(p);
        _pfiletype.setTextSize(12);
        
        _psepline = new Paint();
        _psepline.setColor(Color.BLACK); 
        _psepline.setStrokeWidth(1); 
        _psepline.setStyle(Style.STROKE);
        _psepline.setAntiAlias(true);
        
        _pboldline = new Paint(_psepline);
        _pboldline.setStrokeWidth(3); 
        
        this.setMinimumWidth(WIDTH);
        this.setMinimumHeight(HEIGHT);
         
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.height = HEIGHT;
        lp.width = WIDTH;
        
       
        this.setLayoutParams(lp);
        
        
    } 
    
	public void setItem(IItem i)
	{
		_item = i;
		
		if (_item==null) return;
		
		if (_item.getType() == ItemType.Header)
			_pauth =_pseries;
		
    	_positions =  POSTAB3;
    	if (_item.getSeries().length()==0) _positions = POSTAB2;
    	if ((_item.getAuthor().length()==0) && (_item.getSeries().length()==0)) _positions = POSTAB1;
		
	}
	    
	public IItem getItem()
	{
		return _item;
	}
	

    final int MARGIN =5;
    final int RECTOFFSET = 10;
    final int XPOS1 = HEIGHT - RECTOFFSET*2 + MARGIN;
    final int PICSIZE = 0;
    final int XPOS2 = XPOS1+PICSIZE+ MARGIN;    
    final int TITLEMOVE = 0;
    final int AUTHMOVE = 1;
  
    final float RECTROUND = 5;
    
    static int[] POSTAB3 = new int[]{28,44,62};
    static int[] POSTAB2 = new int[]{30,0,55};
    static int[] POSTAB1 = new int[]{45,0,0};
	
    

    protected void DrawText(Canvas canvas)
    {
    	if (null == _item)
    		return;
    	Rect tb = new Rect();
    	String title = _item.getTitle();
    	_ptitle.setTextSize(26);
    	_ptitle.getTextBounds(title, 0, title.length(), tb);
    	while (tb.width()+XPOS2 > WIDTH) {
    		float fontSize = _ptitle.getTextSize() - 1;
    		_ptitle.setTextSize(fontSize);
    		if (fontSize <= 10)
    			break;
        	_ptitle.getTextBounds(title, 0, title.length(), tb);
    	}
   		canvas.drawText(title, XPOS2, _positions[0], _ptitle);
    	
    	String series = _item.getSeries();
      	Rect sb = new Rect();
       	_pseries.getTextBounds(series, 0, series.length(), sb);
       	if (!Util.IsNullOrEmpty(series)) {
        	sb.set(sb.left, sb.top, sb.right + RECTOFFSET, sb.bottom);
    	}

       	String a = _item.getAuthor();
    	if (!Util.IsNullOrEmpty(a))
    	{
    		_pauth.setTextSize(20);
    		_pauth.getTextBounds(a, 0, a.length(), tb);
        	while (tb.width()+sb.width()+XPOS2 > WIDTH) {
        		float fontSize = _pauth.getTextSize() - 1;
        		_pauth.setTextSize(fontSize);
        		if (fontSize <= 17)
        			break;
        		_pauth.getTextBounds(a, 0, a.length(), tb);
        	}
        	if (tb.width()+sb.width()+XPOS2 > WIDTH) {
        		if (_item.getAuthorCount() > 2) {
        			String[] a2 = a.split(",");
        			a = a2[0].trim() + ", " + a2[1].trim() + ", ...";
            		_pauth.setTextSize(20);
            		_pauth.getTextBounds(a, 0, a.length(), tb);
                	while (tb.width()+sb.width()+XPOS2 > WIDTH) {
                		float fontSize = _pauth.getTextSize() - 1;
                		_pauth.setTextSize(fontSize);
                		if (fontSize <= 17)
                			break;
                		_pauth.getTextBounds(a, 0, a.length(), tb);
                	}
        		}
        	}
        	if (tb.width()+sb.width()+XPOS2 > WIDTH) {
        		if (_item.getAuthorCount() > 1) {
        			String[] a2 = a.split(",");
        			a = a2[0].trim() + ", ...";
            		_pauth.setTextSize(20);
            		_pauth.getTextBounds(a, 0, a.length(), tb);
                	while (tb.width()+sb.width()+XPOS2 > WIDTH) {
                		float fontSize = _pauth.getTextSize() - 1;
                		_pauth.setTextSize(fontSize);
                		if (fontSize <= 17)
                			break;
                		_pauth.getTextBounds(a, 0, a.length(), tb);
                	}
        		}
        	}
           	if (!Util.IsNullOrEmpty(series)) {
	        	if (tb.width()+sb.width()+XPOS2 > WIDTH) {
	    			String[] s2 = _item.getSeriesName().split(" ");
	    			if (s2.length > 3) {
		    			series = s2[0] + " " + s2[1] + " " + s2[2] + "...";
		    			if (_item.getSeriesIndex() != 0)
		    				series += " [" + _item.getSeriesIndex() + "]"; 
		    	       	_pseries.getTextBounds(series, 0, series.length(), sb);
		        		_pauth.setTextSize(20);
		        		_pauth.getTextBounds(a, 0, a.length(), tb);
		            	while (tb.width()+sb.width()+XPOS2 > WIDTH) {
		            		float fontSize = _pauth.getTextSize() - 1;
		            		_pauth.setTextSize(fontSize);
		            		if (fontSize <= 17)
		            			break;
		            		_pauth.getTextBounds(a, 0, a.length(), tb);
		            	}
	    			}
	        	}
           	}
        	if (tb.width()+sb.width()+XPOS2 > WIDTH) {
        		if (_item.getAuthorCount() == 1) {
        			a = a.substring(0, a.lastIndexOf(" ") + 2) + ".";
            		_pauth.setTextSize(20);
            		_pauth.getTextBounds(a, 0, a.length(), tb);
                	while (tb.width()+sb.width()+XPOS2 > WIDTH) {
                		float fontSize = _pauth.getTextSize() - 1;
                		_pauth.setTextSize(fontSize);
                		if (fontSize <= 17)
                			break;
                		_pauth.getTextBounds(a, 0, a.length(), tb);
                	}
        		}
        	}
    		canvas.drawText(a, XPOS2, _positions[2], _pauth);
           	if (!Util.IsNullOrEmpty(series)) {
            	canvas.drawText(series, WIDTH - sb.width(), HEIGHT - 6, _pseries);    		
        	}
    	}
    	
    	String ae = _item.getAuthorExtra();
    	if (ae.length() != 0)
            canvas.drawText(ae, XPOS2, _positions[1], _pseries);

    }
    
    
    protected void onDraw(Canvas canvas) 
    { 

    	if (_item==null) return;
   	
    	String s = 	String.format("%d", _index + 1);
    	RectF rect = new RectF(0, RECTOFFSET,HEIGHT - RECTOFFSET*2,HEIGHT- RECTOFFSET);
    	canvas.drawRoundRect(rect, RECTROUND, RECTROUND, _pboldline);
    	Rect bounds = new Rect();
    	_pindex.getTextBounds(s,0,s.length(),bounds);
    	Rect filetypeBounds = new Rect();
    	String fileType = _item.getType().toString();
    	_pfiletype.getTextBounds(fileType, 0, fileType.length(), filetypeBounds);
    	canvas.drawText( 	s,
    						(rect.width() - bounds.width())/2 +rect.left,
    						(rect.height() - bounds.height() - filetypeBounds.height() - 4)/2 +rect.top+bounds.height(), 
    						_pindex);
    	canvas.drawText( 	fileType,
				(rect.width() - filetypeBounds.width())/2 +rect.left,
				(rect.height() - bounds.height() - filetypeBounds.height() - 4)/2 +rect.top+bounds.height()+filetypeBounds.height() + 4, 
				_pfiletype);
    	if (_index == 0)
    		canvas.drawLine(0, 0, WIDTH,0, _psepline);
    	if (_index < 9)
    		canvas.drawLine(0, HEIGHT - 1, WIDTH, HEIGHT - 1, _psepline);
    	
    	DrawText(canvas);
    	
    } 
}
